package com.hp.ssmp.controller;

import com.hp.ssmp.entity.TCart;
import com.hp.ssmp.entity.TOrderDetails;
import com.hp.ssmp.entity.dto.TCartDto;
import com.hp.ssmp.service.CartService;
import com.hp.ssmp.service.DetailsService;
import com.hp.ssmp.util.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    //    购物车结算
    @GetMapping("/account")
    public R account(Long userid, Long addressid) {
        try {
            cartService.account(userid, addressid);
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/getBy")
    public R<List<TCartDto>> getBy(Long userid) {
        List<TCartDto> cartDtoList = cartService.getBy(userid);
        return R.success(cartDtoList);
    }

    @GetMapping("/getById")
    public R<TCart> getById(Long detailsId) {
        TCart cart = cartService.getById(detailsId);
        return R.success(cart);
    }

    @PostMapping("/add")
    public R add(TCart cart) {
        cart.setNum(1L);
        boolean result = cartService.save(cart);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @GetMapping("/del")
    public R del(Long cartid) {
        boolean result = cartService.removeById(cartid);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @PostMapping("/update")
    public R update(TCart cart) {
        boolean result = cartService.updateById(cart);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }


}
